#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <db.h>
#define DATABASE "free.db"
#define DBLOCATE "./bdb/"
void print_error(int ret)
{
	if(0 != ret)
	{
		printf("ERROR:%s\n",db_strerror(ret));
	}
}
void init_DBT(DBT* key,DBT* data)
{
	memset(key,0,sizeof(DBT));
	memset(data,0,sizeof(DBT));
}
int main(int argc,char *argv[])
{
	system("mkdir bdb");
	DB_ENV *myEnv;
	DB *dbp;
	int ret;
	u_int32_t env_flags;
	ret = db_env_create(&myEnv, 0);
	print_error(ret);
	env_flags = DB_CREATE | DB_INIT_MPOOL;
	ret = myEnv->open(myEnv,DBLOCATE,env_flags,0);
	print_error(ret);
	ret = db_create(&dbp,myEnv,0);
	print_error(ret);
	ret = dbp->open(dbp,NULL,DATABASE,NULL,DB_BTREE,DB_CREATE,0664);
	print_error(ret);
	//===================================
	DBT key,data;
	init_DBT(&key,&data);

	int i;
	for(i=0;i<200;++i)
	{
		int tmp = 200 - i;

		key.size = sizeof(int);
		key.data = &i;
		data.size = sizeof(int);
		data.data =&tmp;
		//put data
		ret = dbp->put(dbp,NULL,&key,&data,DB_NOOVERWRITE);
		print_error(ret);
	}
	//================================
	if (dbp != NULL)
	{
		dbp->close(dbp, 0);
	}
	if (myEnv != NULL)
	{
		myEnv->close(myEnv, 0);
	}
	return 0;
}
